package com.ruoyi.record.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.record.domain.RecordAttend;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.record.domain.RecordSalaryContent;
import com.ruoyi.record.mapper.RecordSalaryMapper;
import com.ruoyi.record.domain.RecordSalary;
import com.ruoyi.record.service.IRecordSalaryService;
import com.ruoyi.common.core.text.Convert;

/**
 * 工资信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-12
 */
@Service
public class RecordSalaryServiceImpl extends ServiceImpl<RecordSalaryMapper, RecordSalary> implements IRecordSalaryService
{
    /**
     * 查询工资信息
     * 
     * @param salaryId 工资信息主键
     * @return 工资信息
     */
    @Override
    public RecordSalary selectRecordSalaryBySalaryId(Long salaryId)
    {
        return baseMapper.selectRecordSalaryBySalaryId(salaryId);
    }

    /**
     * 查询工资信息列表
     * 
     * @param recordSalary 工资信息
     * @return 工资信息
     */
    @Override
    public List<RecordSalary> selectRecordSalaryList(RecordSalary recordSalary)
    {
        return baseMapper.selectRecordSalaryList(recordSalary);
    }

    /**
     * 新增工资信息
     * 
     * @param recordSalary 工资信息
     * @return 结果
     */
    @Override
    public int insertRecordSalary(RecordSalary recordSalary)
    {
        recordSalary.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(recordSalary);
    }

    /**
     * 修改工资信息
     * 
     * @param recordSalary 工资信息
     * @return 结果
     */
    @Override
    public int updateRecordSalary(RecordSalary recordSalary)
    {
        recordSalary.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(recordSalary);
    }

    /**
     * 批量删除工资信息
     * 
     * @param salaryIds 需要删除的工资信息主键
     * @return 结果
     */
    @Override
    public int deleteRecordSalaryBySalaryIds(String salaryIds)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(Convert.toLongArray(salaryIds)));
    }

    /**
     * 删除工资信息信息
     * 
     * @param salaryId 工资信息主键
     * @return 结果
     */
    @Override
    public int deleteRecordSalaryBySalaryId(Long salaryId)
    {
        return baseMapper.deleteById(salaryId);
    }

    /**
     * 校验工资信息
     *
     * @param recordSalary 工资信息
     * @return 结果
     */
    @Override
    public boolean checkRecordSalary(RecordSalary recordSalary) {
        Long salaryID = StringUtils.isNull(recordSalary.getSalaryId()) ? -1L : recordSalary.getSalaryId();
        RecordSalary info = baseMapper.selectOne(new LambdaQueryWrapper<RecordSalary>()
                .eq(RecordSalary::getSalaryMonth, recordSalary.getSalaryMonth())
                .last("limit 1"));
        if (StringUtils.isNotNull(info) && info.getSalaryId().longValue() != salaryID.longValue())
        {
            return false;
        }
        return true;
    }
}
